Aggregation — polymorphic and polytypic
نویسنده
چکیده
Repeating the work of Meertens [1] we show how to define in a few lines a very general “aggregation” function. It is parameterized by a binary operation ⊕ : t × t → t that performs the elementary aggregation steps on values of type t . The aggregation function, then, aggregates all t constituents of a composite ...t ... value into a single t value; its type is: ...t ... → t . The generality is three-fold. First, type t is arbitrary and may be different for different invocations; so the aggregation is polymorphic: working on many types. Second, expression ...t ... is an arbitrary regular type (built from +, ×, List and so on) and may be different for different invocations; so the aggregation is polytypic, also known as generic: working on many type structures. Third, operation ⊕ may be different for different invocations; by suitable choices and a little preprocessing and postprocessing, the function aggregates the t constituents of a given structure into their summation, or their maximum, or their count, or their average, or their first one, or their longest path, and so on. Footnote Nothing of this material is new; a far more precise and far more general elaboration with far more discussion was given by Meertens [1] in 1996. Our intention is to give a self-contained exposition that is more accessible for the uninitiated reader who wants to see the idea with a minimum of formal details. The main difference with Meerstens’ exposition is our way of dealing with functors: in our exposition the only property of functors that we use is that they map sets to sets. A minor difference is the added elaboration for lists, natural numbers, rose trees, and Maybe values. Thus, this note may be viewed as a 8-page introduction in 2011 to Meertens’ 16-page paper of 1996. Notation. In the context of a structured type, we use xs, ys, zs to vary over structured values, and x , y , z to vary over constituents of a structured value. For example, we might say “a nonempty list xs is built from a value y and a sublist zs”. Furthermore, we let f , g , h vary over functions, and t , a, b, c vary over types. (Type variable t is only more special than a, b, c in that it relates to the parameter ⊕ : t × t → t .) Finally, type varies over type expressions. Introduction. Aggregation is a frequently occurring concept in computing, and is programmed by many people over and over again for many kinds of data structures and many kinds of aggregation. Fortunately, SQL has a built-in facility for aggregation, thus freeing the user from reinventing the wheel, but unfortunately it does so for only a few kinds, namely counting, summation, maximization, minimization, and averaging, and only for sole data structure of SQL, namely tables. Meertens [1] gives one single definition of an aggregation that can be instantiated to many kinds (surpassing SQL) and that works, for arbitrary type t and operation ⊕ : t × t → t , on all of the following types and many more:
منابع مشابه
Polytypic Programming With Ease
A functional polytypic program is one that is parameterised by datatype. Since poly-typic functions are deened by induction on types rather than by induction on values they typically operate on a higher level of abstraction than their monotypic counterparts. However, polytypic programming is not necessarily more complicated than conventional programming. We show that a polytypic function is uni...
متن کاملGeneralizing generalized tries
A trie is a search tree scheme that employs the structure of search keys to organize information. Tries were originally devised as a means to represent a collection of records indexed by strings over a xed alphabet. Based on work by C.P. Wadsworth and others, R.H. Connelly and F.L. Morris generalized the concept to permit indexing by elements of an arbitrary monomorphic datatype. Here we go one...
متن کاملType-safe run-time polytypic programming
Polytypic programming is a way of defining type-indexed operations, such as map, fold and zip, based on type information. Run-time polytypic programming allows that type information to be dynamically computed—this support is essential in modern programming languages that support separate compilation, first-class type abstraction, or polymorphic recursion. However, in previous work we defined ru...
متن کاملPolytypic Functional Programming
Structural polymorphism is a generic programming technique known within the functional programming community under the names of polytypic or datatype-generic programming. In this thesis we show that such a technique conflicts with the principle of data abstraction and propose a solution for reconciliation. More concretely, we show that popular polytypic extensions of the functional programming ...
متن کاملHigher-Order Intensional Type Analysis in Type-Erasure Semantics
Higher-order intensional type analysis is a way of defining type-indexed operations, such as map, fold and zip, based on run-time type information. However, languages supporting this facility are naturally defined with a type-passing semantics, which suffers from a number of drawbacks. This paper, describes how to recast higher-order intensional type analysis in a type-erasure semantics. The re...
متن کاملذخیره در منابع من
با ذخیره ی این منبع در منابع من، دسترسی به آن را برای استفاده های بعدی آسان تر کنید
عنوان ژورنال:
دوره شماره
صفحات -
تاریخ انتشار 2011